﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Timers;

using AFCEPF.BankApplication.BLL;
using AFCEPF.BankApplication.DAL;

using Random = AFCEPF.BankApplication.DAL.Random;


namespace AFCEPF.BankApplication.UI.Console
{
    public class BankingSystemPlayer
    {
        private Timer _timer;
        private Random _random;
        private Logger _logger;

        public IList<string>                 _names = new List<string>();
        private IDictionary<string, string> _countries;

        public IDictionary<string, string> Countries
        {
            get { return _countries; }
            
        }



        public BankingSystemPlayer(int interval)
        {
            _random = new Random();

            _logger = Logger.GetLogger(this);

            Log("Reading input files..");
            
            initListOfNames();
            initListOfCountries();

            Log("Finished reading files..");


            Log("Initializing Timer");

            _timer = new Timer(interval);
            _timer.Elapsed  += OnTimerElapsed;
            _timer.Disposed += OnTimerDisposed;
            _timer.Start();
            
        }




        /// <summary>
        /// Inits the list of names.
        /// </summary>
        protected void initListOfNames()
        {
            
            

            var firstNameOfMales   = File.ReadAllLines("@Data Files/Names_First_Male.txt");
            var firstNameOfFemales = File.ReadAllLines("@Data Files/Names_First_Female.txt");
            var lastNames          = File.ReadAllLines("@Data Files/Names_Last_All.txt");
            
            
            
            Log("Creating Data Sets...");


            foreach (var lastName in lastNames)
            {
                foreach (var maleFirstName in firstNameOfMales)
                {
                    _names.Add(CultureInfo.CurrentCulture.TextInfo.ToTitleCase((maleFirstName.Trim() + " " + lastName.Trim()).ToLower()));
                }
            }

            foreach (var lastName in lastNames)
            {
                foreach (var femaleFirstName in firstNameOfFemales)
                {
                    _names.Add(CultureInfo.CurrentCulture.TextInfo.ToTitleCase((femaleFirstName.Trim() + " " + lastName).ToLower().Trim()));
                }
            }
            

            

        }

        /// <summary>
        /// Inits the list of countries.
        /// </summary>
        private void initListOfCountries()
        {
            _countries = new Dictionary<string, string>();

            var lines = File.ReadAllLines(@"@Data Files/CountryList.txt").AsEnumerable();
            
            var elements = from word in lines
                           where word.Trim() != string.Empty
                           select new
                           {
                               Key = word.Split(';')[0],
                               Name = word.Split(';')[1],
                           };
            

            foreach (var element in elements)
            {
                _countries.Add(element.Key, element.Name);
            }
        }




        /// <summary>
        /// Logs the specified text.
        /// </summary>
        /// <param name="text">The text.</param>
        protected void Log(string text)
        {
            Log("{0},", text);
        }

        /// <summary>
        /// Logs the specified strings with the specified format.
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="args">The args.</param>
        protected void Log(string format, params string[] args)
        {
            if (_logger == null)
            {
                _logger = Logger.GetLogger(new Program());
            }
            _logger.Log(format, args);
        }

        private void OnTimerDisposed(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        protected void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            //Log(e.SignalTime.ToString());
           // BankingFacade.Instance.Banks
        }


    }
}
